package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.TagDao;
import com.leoman.dao.UserDao;
import com.leoman.dao.UserTagDao;
import com.leoman.entity.Tag;
import com.leoman.entity.User;
import com.leoman.entity.UserTag;
import com.leoman.exception.*;
import com.leoman.service.UserTagService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * UserTagServiceImpl
 * Created by 涂奕恒 on 2016/11/23 0023 下午 4:02.
 */
@Service
public class UserTagServiceImpl implements UserTagService {

    @Autowired
    private UserTagDao userTagDao;

    @Autowired
    private TagDao tagDao;

    @Autowired
    private UserDao userDao;

    @Override
    public List<UserTag> findAll() {
        return userTagDao.findAll();
    }

    @Override
    public Page<UserTag> find(int pageNum, int pageSize) {
        return userTagDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<UserTag> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public UserTag getById(int id) {
        return userTagDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        userTagDao.delete(id);
    }

    @Override
    public UserTag create(UserTag userTag) {
        return userTagDao.save(userTag);
    }

    @Override
    public UserTag update(UserTag userTag) {
        return userTagDao.save(userTag);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<UserTag> findListByUserId(Integer userId) {
        return userTagDao.findListByUserId(userId);
    }

    @Override
    @Transactional
    public void iModifyInfo(Integer userId, String tagIds) throws ParamErrorException, UserNotFindException, UserSealStatusException, TagNotFindException, UpdateNumMaxException {
        if (StringUtils.isBlank(tagIds)) {
            throw new ParamErrorException();
        }

        // 查询用户信息
        User user = UserServiceImpl.getUserInfo(userId, userDao);

        // 查询该用户的标签列表
        List<UserTag> userTagList = userTagDao.findListByUserId(userId);

        UserTag userTag;
        String[] ids = tagIds.split(",");

        // 第一个是鲜花或者绿草的id
        Tag defaultTag = tagDao.findOne(Integer.parseInt(ids[0]));

        if (null == defaultTag) {
            throw new TagNotFindException();
        }

        // 处理第一个标签
        if (null != userTagList && userTagList.size() > 0) {
            userTag = userTagList.get(0);
            if (!defaultTag.getContent().equals(userTag.getContent())) {
                if (userTag.getUpdateNum() > 0) {
                    throw new UpdateNumMaxException();
                }

                userTag.setUpdateNum(userTag.getUpdateNum() + 1);
                userTag.setContent(defaultTag.getContent());

                userTagDao.save(userTag);
            }
        } else {
            userTag = new UserTag();
            userTag.setUser(user);
            userTag.setContent(defaultTag.getContent());
            userTag.setIsDefault(Constant.USER_STATUS_YES);

            userTagDao.save(userTag);
        }

        if (null != userTagList && userTagList.size() > 1) {
            // 删除该用户的所有非默认标签
            for (int i = 1; i < userTagList.size(); i++) {
                deleteById(userTagList.get(i).getId());
            }
        }

        Tag tag;
        for (int i = 1; i < ids.length; i++) {
            tag = tagDao.findOne(Integer.parseInt(ids[i]));

            if (null == tag) {
                throw new TagNotFindException();
            }

            userTag = new UserTag();
            userTag.setUser(user);
            userTag.setContent(tag.getContent());

            userTagDao.save(userTag);
        }
    }
}